// pages/self/self.js
const app = getApp()
const util = require("../../utils/util");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        hasUserInfo: false, //false=未登录,true=已登录
        showLoginDialog: false, //false=不显示登录弹窗,true=显示登录弹窗
        showBind: false, //false=显示绑定,true=不显示绑定
        showRenshu: false //显示人数统计,只有管理员(state===3)可以看,其他关闭
    },


    /**
     * 退出
     */
    bindQuit: function () {
        // wx.removeStorageSync('openid');
        // wx.removeStorageSync('userInfo');
        // wx.removeStorageSync('bindflag');
        // wx.removeStorageSync('wxuser');

        try {
            //登录之前清楚所有缓存
            wx.clearStorageSync();
        } catch (e) {
            // Do something when catch error
        }


        app.globalData.openid = '';
        app.globalData.userInfo = null;
        this.setData({
            showBind: false,
            userInfo: null,
            hasUserInfo: false,
            showRenshu: false
        })
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (wx.getStorageSync('bindflag') == 1) {
            this.setData({
                showBind: true
            })

            // 查询用户权限,超级权限显示人数统计按钮
            let user_state = wx.getStorageSync('wxuser').state;
            if (user_state === '3') {
                this.setData({
                    showRenshu: true
                })
            }
        }
        if (app.globalData.userInfo) {
            console.log("登录");
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else {
            console.log("没有登录");
        }
    },

    /**
     * 显示登录弹窗
     */
    showLoginDialog: function () {
        this.setData({
            showLoginDialog: true,
        })
    },

    /**
     * 关闭登录弹窗
     */
    closeDialog: function () {
        this.setData({
            showLoginDialog: false,
        })
    },

    /**
     * 跳转到绑定账号, 登录=跳转,未登录=显示登录对话框
     */
    urlToBinding: function () {
        if (app.globalData.userInfo) {
            wx.navigateTo({
                url: '../binding/binding',
            })
        } else {
            //显示登录对话框
            this.setData({
                showLoginDialog: true
            })
        }
    },

    /**
     * 跳转到修改密码, 登录=跳转,未登录=显示登录对话框
     */
    urlToChangepwd: function () {
        if (app.globalData.userInfo) {
            wx.navigateTo({
                url: '../changepwd/changepwd',
            })
        } else {
            //显示登录对话框
            this.setData({
                showLoginDialog: true
            })
        }
    },

    /**
     * 跳转到详细统计页面
     */
    urlToRenshu() {
        wx.navigateTo({
            url: '../renshu/renshu',
        })
    },

    /**
     * 登录
     */
    getUserInfo: function (e) {

        try {
            //登录之前清楚所有缓存
            wx.clearStorageSync();
        } catch (e) {
            // Do something when catch error
        }

        app.globalData.userInfo = e.detail.userInfo;

        //设置缓存userInfo
        wx.setStorageSync('userInfo', e.detail.userInfo);
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })

        const that = this;
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                const code = res.code;
                const url = app.globalData.httpBase + "/api/xcx/getOpenid?code=" + code;

                util.newhttp(url).then(function (res) {
                    const resData = res.data;
                    if (resData.code == 1) {

                        app.globalData.openid = resData.data.openid;
                        wx.setStorageSync('openid', resData.data.openid);
                        wx.setStorageSync('wxuser', resData.data.wxuser);
                        wx.setStorageSync('bindflag', resData.data.bindflag);
                        //如果是已经绑定,把绑定框隐藏
                        if (resData.data.bindflag == 1) {
                            that.setData({
                                showBind: true
                            })

                            // 查询用户权限,超级权限显示人数统计按钮
                            let user_state = resData.data.wxuser.state;
                            if (user_state === '3') {
                                that.setData({
                                    showRenshu: true
                                })
                            }
                        }
                        // 登录成功,关闭登录弹窗
                        that.setData({
                            showLoginDialog: false
                        })


                    }
                }).catch(function (error) {
                    util.openErrorToast("登录失败!");
                })

            }
        })


    },

})